package com.jrx.anytxn.transaction.job.customer.step;

import com.alibaba.fastjson.JSON;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.customer.dto.customer.CustomerRes;
import com.jrx.anytxn.customer.entity.CmLimitAdjustLog;
import com.jrx.anytxn.customer.mapper.base.CmLimitAdjustLogMapper;
import com.jrx.anytxn.customer.service.ICustomerService;
import com.jrx.anytxn.feign.api.response.CardLogOffRes;
import com.jrx.anytxn.feign.api.service.IUserApiService;
import com.jrx.anytxn.feign.gateway.bean.ApiResponse;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.dto.loan.CardLogOffReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @comment: 验证用户身份证有效期Processor
 * @author: liuzb
 * @date: 2022/06/06 15:00
 */
@Service
public class CardLogOffProcessor implements ItemProcessor<CmLimitAdjustLog, CmLimitAdjustLog> {

    private static final Logger logger = LoggerFactory.getLogger(CardLogOffProcessor.class);

    @Autowired
    private ICustomerService iCustomerService;

    @Autowired
    private IUserApiService iUserApiService;

    @Autowired
    private CmLimitAdjustLogMapper cmLimitAdjustLogMapper;

    @Override
    public CmLimitAdjustLog process(CmLimitAdjustLog cmLimitAdjustLog) throws Exception {
        CustomerRes customerRes = iCustomerService.getCustomerByChannelAndCustomerIdAndTensnId(cmLimitAdjustLog.getChannel(),
                cmLimitAdjustLog.getCustomerId(), cmLimitAdjustLog.getTenantId());

        if (StringUtils.isEmpty(customerRes.getFirstLevel().getEcifId()) ||
                StringUtils.isEmpty(customerRes.getSecondLevel().getUserId())) {
            logger.error("客户信息不全，需检查客户是否已激活，客户id：{}", cmLimitAdjustLog.getCustomerId());
            throw new TxnBizException(TxnRespCode.CUSTOMER_INFO_ERROR);
        }

        CardLogOffReq cardLogOffReq = new CardLogOffReq();
        cardLogOffReq.setChannelNo(TransBizConstant.DEFAULT_CHANNEL_NO);
        cardLogOffReq.setTenantId(TransBizConstant.DEFAULT_TENANT_ID);
        cardLogOffReq.setUserId(customerRes.getSecondLevel().getUserId());
        cardLogOffReq.setEcifId(customerRes.getFirstLevel().getEcifId());

        ApiResponse<CardLogOffRes> apiResponse = iUserApiService.accountLogOff(JSON.toJSONString(cardLogOffReq));
        if (Objects.isNull(apiResponse) || TxnRespCode.SUCCESS.getCode() != apiResponse.getCode() || Objects.isNull(apiResponse.getData())) {
            throw new TxnBizException("请求api兴安花卡销户接口失败");
        }

        if (!"1".equals(apiResponse.getData().getLogOffFlag())) {
            logger.warn("兴安花卡注销失败");
            throw new TxnBizException(TxnRespCode.LOG_OFF_ERROR);
        }

        cmLimitAdjustLog.setCardLogOff("1");//已注销
        cmLimitAdjustLogMapper.updateByPrimaryKeySelective(cmLimitAdjustLog);

        return cmLimitAdjustLog;
    }

}
